package com.ysh.contentcenter.service.content;

import com.ysh.contentcenter.dao.content.ShareMapper;
import com.ysh.contentcenter.dao.messaging.RocketmqTransactionLogMapper;
import com.ysh.contentcenter.domain.dto.content.ShareAuditDTO;
import com.ysh.contentcenter.domain.dto.content.ShareDTO;
import com.ysh.contentcenter.domain.dto.messaging.UserAddBonusMsgDTO;
import com.ysh.contentcenter.domain.dto.user.UserDTO;
import com.ysh.contentcenter.domain.entity.content.Share;
import com.ysh.contentcenter.domain.entity.messaging.RocketmqTransactionLog;
import com.ysh.contentcenter.domain.enums.AuditStatusEnum;
import com.ysh.contentcenter.feignclient.UserCenterFeignClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.BeanUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;
import java.util.UUID;

@Service
@RequiredArgsConstructor
@Slf4j
public class ShareService {
    private final ShareMapper shareMapper;
    private final UserCenterFeignClient userCenterFeignClient;
    private final RocketMQTemplate rocketMQTemplate;
    private final RocketmqTransactionLogMapper rocketmqTransactionLogMapper;

    public ShareDTO findById(Integer id){
        Share share=this.shareMapper.selectByPrimaryKey(id);
        Integer userId = share.getUserId();

        // 使用feign来进行调用
        UserDTO userDTO = this.userCenterFeignClient.findById(userId);

        ShareDTO shareDTO = new ShareDTO();
        BeanUtils.copyProperties(share,shareDTO);
        shareDTO.setWxNickname(userDTO.getWxNickname());

        return shareDTO;
    }

    public Share auditById(Integer id, ShareAuditDTO auditDTO) {
        Share share = this.shareMapper.selectByPrimaryKey(id);
        if(share==null){
            throw new IllegalArgumentException("参数非法！该分享不存在！");
        }
        if(!Objects.equals("NOT_YET",share.getAuditStatus())){
            throw new IllegalArgumentException("参数非法！ 改分享已审核通过或审核未通过");
        }
        // 发送半消息
        if(AuditStatusEnum.PASS.equals(auditDTO.getAuditStatusEnum())){
            String transactionID = UUID.randomUUID().toString();
            this.rocketMQTemplate.sendMessageInTransaction("tx-add-bonus-group","add-bonus", MessageBuilder.withPayload(UserAddBonusMsgDTO.builder()
                    .userId(share.getUserId())
                    .bonus(50)
                    .build())
                    .setHeader(RocketMQHeaders.TRANSACTION_ID, transactionID)
                    .setHeader("share_id",id)
                    .build(),auditDTO);
        }else{
            this.auditByIdInDB(id,auditDTO);
        }


        return share;
    }

    public void auditByIdInDB(Integer id,ShareAuditDTO auditDTO) {
        Share share = Share.builder()
                .id(id)
                .auditStatus(auditDTO.getAuditStatusEnum().toString())
                .reason(auditDTO.getReason())
                .build();
        this.shareMapper.updateByPrimaryKeySelective(share);
    }

    @Transactional(rollbackFor = Exception.class)
    public void auditByIdWithRocketMqLog(Integer id,ShareAuditDTO auditDTO,String transactionID){
        this.auditByIdInDB(id,auditDTO);
        this.rocketmqTransactionLogMapper.insertSelective(
                RocketmqTransactionLog.builder()
                        .transactionId(transactionID)
                        .log("审核分享...")
                        .build()
        );
    }


}
